package com.vf.admin.client.controller.oppo.controller;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * <p>Title : </p>
 * <p>Description : </p>
 * <p>DevelopTools : IDEA</p>
 * <p>DevelopSystem : Windows11</p>
 *
 * @Author : zxw
 * @Date 2023/8/28 15:11
 */
public class OppoTest {

    public static void main(String[] args) {


        String url = "https://api.ads.heytapmobi.com/api/uploadActiveData"; // 替换成实际的POST接口地址
        String data = "param1=value1&param2=value2"; // 替换成实际要发送的参数

        try {
            URL urlObj = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();

            // 设置请求方法为POST
            connection.setRequestMethod("POST");

            // 允许写入数据
            connection.setDoOutput(true);

            // 设置请求体的内容类型为表单数据
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("timestamp", String.valueOf(System.currentTimeMillis()));
            connection.setRequestProperty("signature", String.valueOf(System.currentTimeMillis()));


            // 获取输出流并写入请求参数数据
            DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
            outputStream.writeBytes(data);
            outputStream.flush();
            outputStream.close();

            // 获取响应状态码
            int responseCode = connection.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 读取响应数据
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
                String line;
                StringBuilder response = new StringBuilder();

                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }

                reader.close();

                // 输出响应结果
                System.out.println("请求成功，响应结果为：" + response.toString());
            } else {
                System.out.println("请求失败，错误码为：" + responseCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
